import { forwardRef, useRef } from "react";
// 子组件
const Input = forwardRef((_props, ref) => {
  return (
    <>
      <input type="text" ref={ref} />
    </>
  );
});
// 父组件
function App() {
  const inputRef = useRef(null);
  // 需求：点击按钮，让子组件的input获取焦点
  const changeFocus = () => {
    console.log(inputRef.current); // <input type="text" />
    inputRef.current.focus();
  };
  return (
    <>
      <Input ref={inputRef} />
      <button onClick={changeFocus}>focus</button>
    </>
  );
}

export default App;
